#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#include <sys/time.h>
#include <time.h>
#include "pfring.h"

#define DEFAULT_SNAPLEN 1536

void print_packet(const struct pfring_pkthdr *h, const u_char *p, u_int8_t dump_match)
{
    u_char *p_tmp = p;
    if (*(p_tmp + 23) != 1)
        return;

    for (int i = 0; i < 34; i++)
    {
        printf("%x ", *(p_tmp + i));

        if (i == 13)
            printf("\n");
        if (i > 14 && (i - 14) % 4 == 3)
            printf("\n");
    }

    // printf("%d bytes from %s:icmp_seq=%d time=%.3f ms\n",addr_len,inet_ntoa(ip_header.sour_ip),htons(icmp.seq),usec_interval);
}

int main(int argc, char *argv[])
{
    char *device_name = "ens33";
    u_int32_t flags = 0;
    pfring *ring = pfring_open(device_name, DEFAULT_SNAPLEN, flags);
    if (ring == NULL)
        return -1;
    // pfring_loop2(ring, print_packet, (u_char *)NULL, 1);
    struct pfring_pkthdr hdr;
    u_char *buffer = NULL;
    int rc = 0;

    memset(&hdr, 0, sizeof(hdr));
    ring->break_recv_loop = ring->break_recv_loop_ext = 0;

    if ((!ring) || ring->is_shutting_down || (!ring->recv) || ring->mode == send_only_mode)
        return -1;

    while (!ring->break_recv_loop_ext)
    {
        rc = ring->recv(ring, &buffer, 0, &hdr, 1);
        // pfring_recv(ring, &buffer, 0, &hdr, 1);
        print_packet(&hdr, buffer, NULL);
    }

    return 0;
}